package com.utils.socket.tcp;

import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by apple on 2017/4/23.
 */
public class TCPServer {
    // 建立ServerSocket，并设置其端口号
    private ServerSocket ss;

    public static final int port = 8962;

    public TCPServer() {

        try {

            ss = new ServerSocket(port);

        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    public void setConnection() throws IOException {
        // 建立服务器端的Socket
        Socket s;
        OutputStream os;
        try { // ServerSocke.accept()t返回一个Socket对象

            s = ss.accept();
            os = s.getOutputStream();
            os.write("hello".getBytes());
            os.close();
            s.close();
        } catch (IOException e) {

            e.printStackTrace();
        }

    }

    public static void main(String[] args) throws IOException {

        TCPServer ms = new TCPServer();
        ms.setConnection();

    }
}
